package com.wavefax.minbang.xu.learning.servlet01;

import com.alibaba.fastjson.JSONObject;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


//接收content-type为application/json类型的请求参数，并将参数以json格式输出到控制台
//配置log4j2，将请求参数输出到日志文件
public class MyServletTwo extends HttpServlet {


    {
        //指明properties文件的路径(该代码只需要出现一次，以后不需要再出现)
        //org.apache.log4j.LogManager.resetConfiguration();
        //PropertyConfigurator.configure("D:/班级文件/大四上/毕设项目/LearningCode/src/main/resources/log4j.xml");
    }
    Logger LOG = LogManager.getRootLogger();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //将请求param的参数以json格式输出到控制台和日志文件
        Map map = req.getParameterMap();
        //将Map<String,String[]>转化为HashMap<String,String>
        Map returnMap = new HashMap();
        Iterator entries = map.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if(null == valueObj){
                value = "";
            }else if(valueObj instanceof String[]){
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1);
            }else{
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        JSONObject jsonObject = new JSONObject(map);
        System.out.println("接收content-type为application/json或application/x-www-form-urlencoded类型的请求头的参数，并以json格式输出到控制台：");
        System.out.println(jsonObject);
        LOG.info("从前端接收到的请求的请求头的参数：" + jsonObject);



        //将请求body的参数以json格式输出到控制台和日志文件
        JSONObject json = new JSONObject();
        try {
            InputStreamReader in = new InputStreamReader(req.getInputStream(), "utf-8");
            BufferedReader br = new BufferedReader(in);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            json = JSONObject.parseObject(sb.toString());
            //将请求参数输出到日志文件
            LOG.info("从前端接收到的请求的body参数：" + json);
            System.out.println("接收content-type为application/json或application/x-www-form-urlencoded类型的请求的body参数，并以json格式输出到控制台：");
            System.out.println(json);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

    }

}
